package org.hawk.gwt.ppc.scanner.utils;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

final class ArraysTypeScope extends JavaTypeScope {
	private JavaClassDescriptor classDescriptor;

	ArraysTypeScope(JavaClassDescriptor classDescriptor) {
		super(null, 0, new LinkedList<JavaVariable>(), null);
		this.classDescriptor = classDescriptor;
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaTypeScope#getVariables(int)
	 */
	@Override
	public List<JavaVariable> getVariables(int index) {
		return Collections.singletonList(
			new JavaVariable("length", "int", 0, this));
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaTypeScope#getVariable(int, java.lang.String)
	 */
	@Override
	public JavaVariable getVariable(int index, String name) {
		if(!name.equals("length")){
			return null;
		}
		return getVariables(index).get(0);
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaTypeScope#getMethods()
	 */
	@Override
	public List<JavaMethod> getMethods() {
		return Collections.emptyList();
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaTypeScope#getMethod(java.lang.String, java.lang.String[])
	 */
	@Override
	public JavaMethod getMethod(String name, String... parametersTypes) {
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaScope#getParent()
	 */
	@Override
	public JavaScope getParent() {
		return null;
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaScope#getScope(int)
	 */
	@Override
	public JavaScope getScope(int index) {
		return this;
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaScope#getChildren()
	 */
	@Override
	public List<JavaScope> getChildren() {
		return Collections.emptyList();
	}
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if(this==obj)return true;
		if(obj instanceof ArraysTypeScope){
			return ((ArraysTypeScope)obj)
				.classDescriptor.equals(classDescriptor);
		}
		return false;
	}
	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return classDescriptor.toString();
	}
	/*
	 * (non-Javadoc)
	 * @see org.hawk.gwt.ppc.scanner.utils.JavaTypeScope#isAssignableFrom(org.hawk.gwt.ppc.scanner.utils.JavaTypeScope)
	 */
	@Override
	public boolean isAssignableFrom(JavaTypeScope type) {
		if(type instanceof ArraysTypeScope){
			ArraysTypeScope array = (ArraysTypeScope)type;
			return classDescriptor.getCompilationUnit().isAssignableFrom(
					array.classDescriptor.getCompilationUnit());
		}
		return false;
	}
}
